
<template>
  <div>夏宇航
    <el-card>
      <!-- 搜索表单域 -->
      <el-form :model="formDara" label-width="80px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="标签名称">
              <el-input style="width: 330px" v-model="formDara.tags"></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select style="width: 330px" v-model="formDara.state" placeholder="请选择">
                <el-option v-for="item in tateOptions" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="城市">
              <el-select style="width: 330px" placeholder="请选择" @change="hanldercity()" v-model="formDara.province">
                <el-option v-for="item in citys" :key="item.value" :label="item.label" :value="item">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label-width="40px">
              <el-button size="mini" @click="resetForm">清除</el-button>
              <el-button size="mini" @click="searchList" type="primary">搜索</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="地区">
              <el-select style="width: 330px" placeholder="请选择" v-model="formDara.city">
                <el-option v-for="item in citysson" :key="item.value" :label="item.label" :value="item">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="企业简称">
              <el-input style="width: 330px" v-model="formDara.shortName"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="success" @click="addUser" style="float:right" icon="el-icon-edit" size="mini">新增用户
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-alert :title="'共'+counts+'条记录'" :data="listData" show-icon type="info">
      </el-alert>
      <!-- 表格 -->
      <div class="table">
        <template>
          <el-table style="width: 100%" stripe :data="listData">
            <el-table-column fixed label="序号" prop="id" width="150">
            </el-table-column>
            <el-table-column label="企业编号" prop="number">
            </el-table-column>
            <el-table-column label="企业简称" prop="shortName">
            </el-table-column>
            <el-table-column label="标签" width="150" prop="tags">
            </el-table-column>
            <el-table-column label="创建者" prop="creatorID">
            </el-table-column>
            <el-table-column label="创建日期" prop="addDate">
              <template v-slot="{ row }">
                {{ row.addDate| parseTimeByString }}
              </template>
            </el-table-column>
            <el-table-column label="备注" prop="remarks">
            </el-table-column>
            <el-table-column label="状态" prop="state">
              <template slot-scope="{row}">
                <span v-if="row.state === 1">启用</span>
                <span v-else>禁用</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="150">
              <template slot-scope="{row}">
                <el-button type="primary" @click="setList(row)" icon="el-icon-edit" plain circle></el-button>
                <el-button type="success" v-if="row.state === 0" icon="el-icon-check" @click="setState(row)" plain
                  circle></el-button>
                <el-button type="danger" v-else icon="el-icon-close" plain circle @click="setState(row)"></el-button>
                <el-button type="danger" icon="el-icon-delete" @click="Delete(row)" plain circle></el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
        <!-- 分页 -->
        <div class="block">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="queryInfo.page" :page-sizes="[5, 10, 20, 40]" :page-size="queryInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper" :total="counts">
          </el-pagination>
        </div>
      </div>
    </el-card>
    <!-- 添加用户对话框 -->
    <Add :titleInfo='titleInfo' :formBase="formBase" v-on:newDataes="newDataes" ref="addRef" v-on:xiugai='xiugai'
      v-on:newdialog='newdialog'>
    </Add>
    <!-- <el-dialog title="提示" :visible.sync="addDialogVisible" width="50%" :before-close="addhandleClose()">
      <div class="addform">
        <el-form label-width="80px" ref="addForm" :model="addUserForm" :rules="rules">
          <el-form-item label="企业名称" prop="name">
            <el-input></el-input>
          </el-form-item>
          <el-form-item label="所属公司" prop="company">
            <el-input></el-input>
          </el-form-item>
          <el-form-item label='城市' prop="city">
            <el-col :span="12">
              <el-select placeholder="请选择" @change="hanldercity" v-model="citysValue">
                <el-option v-for="item in citys" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="12">
              <el-select placeholder="请选择">
                <el-option>
                </el-option>
              </el-select>
            </el-col>
          </el-form-item>
          <p>https://www.tianyancha.com （在此可查询所属公司全称及简称）</p>
          <el-form-item label="方向(企业标签)" prop="enterprise">
            <el-input></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import { list, remove, disabled } from '../../api/hmmm/companys'
import { provinces, citys } from '../../api/hmmm/citys'
import Add from '../components/companys-add.vue'
export default {
  components: { Add: Add },
  data () {
    return {
      // 搜索表单数据

      // 子组件添加用户对话框
      formBase: {
        shortName: '',
        isFamous: '',
        company: '',
        province: '',
        city: '',
        tags: '',
        remarks: ''
      },
      titleInfo: {
        text: '添加',
        pageTitle: '用户'
      },
      dialogFormVisible: false,
      citys: [], // 城市数据
      citysson: [], // 下级市数据

      // // 表单验证
      // rules: {
      //   name: [
      //     { required: true, message: '请输入企业名称', trigger: 'blur' }
      //   ],
      //   company: [
      //     { required: true, message: '请输入公司名称', trigger: 'blur' }
      //   ],
      //   enterprise: [
      //     { required: true, message: '请输入方向', trigger: 'blur' }
      //   ],
      //   remark: [
      //     { required: true, message: '请输入备注', trigger: 'blur' }
      //   ],
      //   city: [
      //     { required: true, message: '请输入城市', trigger: 'blur' }
      //   ]
      // },
      addUserForm: {},
      // addDialogVisible: false, // 添加用户对话框
      listData: [],
      queryInfo: {
        page: 1, // 当前页
        pagesize: 10 // 每页显示几条
      },
      formDara: {
        state: '', // 状态复选框value
        tags: '',
        city: '',
        shortName: '',
        province: ''
      },
      counts: 0, // 总条数,
      // 状态复选框
      tateOptions: [{
        value: '1',
        label: '启动'
      }, {
        value: '0',
        label: '禁用'
      }]
    }
  },

  created () {
    this.getenterList(this.queryInfo)
    this.citys = provinces()
  },
  watch: {
    counts () {
      if (this.counts === (this.queryInfo.page - 1) * this.queryInfo.pagesize && this.counts !== 0) { // 这一行是关键代码，倍数关系
        this.queryInfo.page -= 1
        this.getenterList(this.queryInfo)// 获取表格数据的方法
      }
    }
  },

  methods: {
    // 城市选择器change事件
    hanldercity () {
      this.citysson = citys(this.formDara.province)
      this.formDara.city = this.citysson[0]
    },
    searchList () {
      this.getenterList({ ...this.queryInfo, ...this.formDara })
    },
    setList (row) {
      this.titleInfo.text = '修改'
      this.formBase = row
      // this.formBase.forEach(item => {
      //   if (item.isFamous === 1) {
      //     item.isFamous = true
      //   } else {
      //     item.isFamous = false
      //   }
      // })
      // console.log(this.formBase)
      this.$refs.addRef.dialogFormV()
      // this.dialogFormVisible = true
    },
    // 接收子组件的值
    newDataes (res) {
      // delete res.isFamous
      console.log(res)
      this.$message.success('添加成功！')
      this.getenterList(this.queryInfo)
    },
    xiugai (res) {
      this.$message.success('修改成功！')
      this.getenterList(this.queryInfo)
    },
    newdialog (newdata) {
      this.dialogFormVisible = newdata
    },
    addUser () {
      this.titleInfo.text = '添加'
      this.formBase = {
        shortName: '',
        isFamous: false,
        company: '',
        province: '',
        city: '',
        tags: '',
        remarks: ''
      }
      this.$refs.addRef.dialogFormV()
      // this.dialogFormVisible = true
    },
    // 更新列表状态
    setState (row) {
      const state = row.state === 0 ? '启动' : '禁用'
      this.$confirm('已成功' + state + ',是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const rows = row.state === 0 ? 1 : 0
        row.state = rows
        await disabled(row).then(res => {
          if (res.data.success !== true) return this.$message.console.error('更新失败')
          this.$message.success('更新成功')
          this.getenterList(this.queryInfo)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 列表删除
    Delete (row) {
      this.$confirm('此操作将永久删除该企业, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        console.log(row)
        await remove(row).then(res => {
          console.log(res)
          if (res.data.success !== true) return this.$message.console.error('删除失败')
          this.$message.success('删除成功')
        })
        this.getenterList(this.queryInfo)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 表单重置
    resetForm () {
      this.formDara = {
        state: '', // 状态复选框value
        tags: '',
        city: '',
        shortName: '',
        province: ''
      }
      this.getenterList(this.queryInfo)
    },
    // 分页切换
    handleSizeChange (newsize) {
      this.queryInfo.pagesize = newsize
      this.getenterList(this.queryInfo)
    },
    handleCurrentChange (newpagenum) {
      this.queryInfo.page = newpagenum
      this.getenterList(this.queryInfo)
    },
    // 初始化列表数据
    async getenterList (data) {
      await list(data).then(res => {
        this.listData = res.data.items
        this.counts = res.data.counts
        console.log(res)
      })
    }
  }
}
</script>

<style scoped lang='less'>
.el-card {
  margin: 20px;
}

.table {
  margin-top: 20px;
}
.block {
  display: flex;
  justify-content: flex-end;
  margin-top: 15px;
}
.addform {
  width: 80%;
  margin: auto;
  p {
    text-align: center;
  }
}
</style>
